package treasurer;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import secretary.AddStudentAccount;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.Account;
import controllers.Accountability;
import controllers.AccountabilityItem;
import controllers.BKCashAdvance;
import controllers.BKPayroll;
import controllers.Logs;
import controllers.Registered;
import controllers.User;

public class AddAccountability extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	Account semester = (Account)sqlMap.queryForObject("Account.getSemester", 1);
	private Model descModel = new Model();
	private Model amountModel = new Model();
	private Model semesterModel = new Model(semester.getSemester());
	Account schoolyear = (Account)sqlMap.queryForObject("Account.getSchoolYear", 1);
	private Model schoolYearModel = new Model(schoolyear.getSchoolyear());
	private Model accountableModel = new Model();
	private Model quantityModel = new Model();
	private DropDownChoice accountable;
	private List choice = Arrays.asList(new String[] 
	{ "CS Officers", "CS-President", "CS-Secretary", "CS-Treasurer", "1st Year", "2nd Year", "3rd Year", "4th Year", "All" });
	
	public AddAccountability() throws SQLException, IOException
	{
		TextField desc = new TextField("description", descModel);
		desc.setRequired(true);
		
		TextField amount = new TextField("amount", amountModel);
		amount.setRequired(true);
		
		TextField semester = new TextField("semester", semesterModel);
		semester.setRequired(true);
		
		TextField schoolyear = new TextField("schoolyear", schoolYearModel);
		schoolyear.setRequired(true);
		
		TextField quantity = new TextField("quantity", quantityModel);
		
		accountable = new DropDownChoice("accountable", accountableModel, choice);
		
		Form form = new Form("addAccountabilityItem");
		
		form.add(new Button("add"){
			
			private static final long serialVersionUID = 1L;

			public void onSubmit(){
				
			try {	
				String desc = (String) descModel.getObject();
				float amount = Float.valueOf((String)(amountModel.getObject()));
				String semester = (String) semesterModel.getObject();
				String schoolyear = (String) schoolYearModel.getObject();
				String accountable = (String)accountableModel.getObject();
				float quantity = Float.valueOf((String)quantityModel.getObject());
						
				String uploader = ((Sessions)getSession()).getDesc();
				
				Date today = new Date();
				Timestamp timestamp = new Timestamp(today.getTime());
				String ts = timestamp.toString();

				if(accountable == "CS Officers")
				{	
				AccountabilityItem acc = new AccountabilityItem();
				acc.setAccountable(accountable);
				acc.setDate_created(ts);
				acc.setItem_amount(amount);
				acc.setItem_desc(desc);
				acc.setItem_quantity(quantity);
				acc.setItem_schoolyear(schoolyear);
				acc.setItem_sem(semester);
				acc.setUploader(uploader);
				sqlMap.insert("AccountabilityItem.insert", acc);
				
				
				
				for(int i=1;i<4;i++)
				{
				Account account = (Account) sqlMap.queryForObject("Account.getStudent", i);
				String id= account.getStudent_id(i);
				Accountability a = new Accountability();
				a.setAccountability_desc(desc);
				a.setDate_created(ts);
				a.setAmount(amount);
				a.setSchoolyear(schoolyear);
				a.setSemester(semester);
				a.setStatus("not cleared");
				a.setUploader(uploader);
				a.setStudent_id(id);
				sqlMap.insert("Accountability.insert", a);
				
				Logs log = new Logs();
				log.setLog_activity("Add Accountabiity:" + id + " " + desc);
				log.setLog_officer(uploader);
				log.setLog_time(ts);
				
				sqlMap.insert("Logs.insert", log);
				
				AccountabilityItemList page = new AccountabilityItemList();
				setResponsePage(page);
				}
				
				
				
				
				AccountabilityItemList page = new AccountabilityItemList();
				setResponsePage(page);
				error("added.");
				}
				
				else if(accountable == "CS-President")
				{
					{	
						AccountabilityItem acc = new AccountabilityItem();
						acc.setAccountable(accountable);
						acc.setDate_created(ts);
						acc.setItem_amount(amount);
						acc.setItem_desc(desc);
						acc.setItem_quantity(quantity);
						acc.setItem_schoolyear(schoolyear);
						acc.setItem_sem(semester);
						acc.setUploader(uploader);
						sqlMap.insert("AccountabilityItem.insert", acc);
						
					
						//Account account = new Account();
						Account account = (Account) sqlMap.queryForObject("Account.getStudent", 1);
						String id = account.getStudent_id(1);
						
						
						Accountability a = new Accountability();
						a.setAccountability_desc(desc);
						a.setDate_created(ts);
						a.setSchoolyear(schoolyear);
						a.setSemester(semester);
						a.setStatus("not cleared");
						a.setUploader(uploader);
						a.setStudent_id(id);
						a.setAmount(amount);
						sqlMap.insert("Accountability.insert", a);
						
						Logs log = new Logs();
						log.setLog_activity("Add Accountabiity:" + id + " " + desc);
						log.setLog_officer(uploader);
						log.setLog_time(ts);
						
						sqlMap.insert("Logs.insert", log);
						
						AccountabilityItemList page = new AccountabilityItemList();
						setResponsePage(page);
						}
					
					
				}
				
				else if(accountable == "CS-Secretary")
				{
					{	
						AccountabilityItem acc = new AccountabilityItem();
						acc.setAccountable(accountable);
						acc.setDate_created(ts);
						acc.setItem_amount(amount);
						acc.setItem_desc(desc);
						acc.setItem_quantity(quantity);
						acc.setItem_schoolyear(schoolyear);
						acc.setItem_sem(semester);
						acc.setUploader(uploader);
						sqlMap.insert("AccountabilityItem.insert", acc);
						
					
						Account account = (Account) sqlMap.queryForObject("Account.getStudent", 2);
						String id= account.getStudent_id(2);
						
						Accountability a = new Accountability();
						a.setAccountability_desc(desc);
						a.setDate_created(ts);
						a.setSchoolyear(schoolyear);
						a.setSemester(semester);
						a.setAmount(amount);
						a.setStatus("not cleared");
						a.setUploader(uploader);
						a.setStudent_id(id);
						sqlMap.insert("Accountability.insert", a);
						
						
						Logs log = new Logs();
						log.setLog_activity("Add Accountabiity:" + id + " " + desc);
						log.setLog_officer(uploader);
						log.setLog_time(ts);
						
						sqlMap.insert("Logs.insert", log);
						
						AccountabilityItemList page = new AccountabilityItemList();
						setResponsePage(page);
						}
					
					
				}
				else if(accountable == "CS-Treasurer")
				{
					{	
						AccountabilityItem acc = new AccountabilityItem();
						acc.setAccountable(accountable);
						acc.setDate_created(ts);
						acc.setItem_amount(amount);
						acc.setItem_desc(desc);
						acc.setItem_quantity(quantity);
						acc.setItem_schoolyear(schoolyear);
						acc.setItem_sem(semester);
						acc.setUploader(uploader);
						sqlMap.insert("AccountabilityItem.insert", acc);
						
					
						Account account = (Account) sqlMap.queryForObject("Account.getStudent", 3);
						String id= account.getStudent_id(3);
						
						
						Accountability a = new Accountability();
						a.setAccountability_desc(desc);
						a.setDate_created(ts);
						a.setSchoolyear(schoolyear);
						a.setSemester(semester);
						a.setAmount(amount);
						a.setStatus("not cleared");
						a.setUploader(uploader);
						a.setStudent_id(id);
						sqlMap.insert("Accountability.insert", a);
						
						
						Logs log = new Logs();
						log.setLog_activity("Add Accountabiity:" + id + " " + desc);
						log.setLog_officer(uploader);
						log.setLog_time(ts);
						
						sqlMap.insert("Logs.insert", log);
						
						AccountabilityItemList page = new AccountabilityItemList();
						setResponsePage(page);
						}
				}
				else if(accountable == "All")
				{
					{	
				
						AccountabilityItem acc = new AccountabilityItem();
						acc.setAccountable(accountable);
						acc.setDate_created(ts);
						acc.setItem_amount(amount);
						acc.setItem_desc(desc);
						acc.setItem_quantity(quantity);
						acc.setItem_schoolyear(schoolyear);
						acc.setItem_sem(semester);
						acc.setUploader(uploader);
						sqlMap.insert("AccountabilityItem.insert", acc);
						
						List<Registered> list = (List<Registered>)sqlMap.queryForList("Registered.getAllRegistered", semester);
						Registered reg = null;
					    for (Registered b : list) {
					    	
					    	String id =  b.getStudent_id();
					      	Accountability a = new Accountability();
							a.setAccountability_desc(desc);
							a.setDate_created(ts);
							a.setSchoolyear(schoolyear);
							a.setSemester(semester);
							a.setAmount(amount);
							a.setStatus("not cleared");
							a.setUploader(uploader);
							a.setStudent_id(id);
							sqlMap.insert("Accountability.insert", a);
							
							
							Logs log = new Logs();
							log.setLog_activity("Add Accountabiity:" + id + " " + desc);
							log.setLog_officer(uploader);
							log.setLog_time(ts);
							
							sqlMap.insert("Logs.insert", log);
					      
					    }
			
						AccountabilityItemList page = new AccountabilityItemList();
						setResponsePage(page);
						}
				}
				else if(accountable == "1st Year")
				{
					{	
									
						List<Registered> list = (List<Registered>)sqlMap.queryForList("Registered.getRegisteredByYear", 1);
						Registered reg = null;
					    for (Registered b : list) {
					    	
					    	String id =  b.getStudent_id();
					    	System.out.println(id);
					      	Accountability a = new Accountability();
							a.setAccountability_desc(desc);
							a.setDate_created(ts);
							a.setSchoolyear(schoolyear);
							a.setSemester(semester);
							a.setAmount(amount);
							a.setStatus("not cleared");
							a.setUploader(uploader);
							a.setStudent_id(id);
							sqlMap.insert("Accountability.insert", a);
							
							
							Logs log = new Logs();
							log.setLog_activity("Add Accountabiity:" + id + " " + desc);
							log.setLog_officer(uploader);
							log.setLog_time(ts);
							
							sqlMap.insert("Logs.insert", log);
					      
					    }
					    
					    AccountabilityItem acc = new AccountabilityItem();
						acc.setAccountable(accountable);
						acc.setDate_created(ts);
						acc.setItem_amount(amount);
						acc.setItem_desc(desc);
						acc.setItem_quantity(quantity);
						acc.setItem_schoolyear(schoolyear);
						acc.setItem_sem(semester);
						acc.setUploader(uploader);
						sqlMap.insert("AccountabilityItem.insert", acc);
			
						AccountabilityItemList page = new AccountabilityItemList();
						setResponsePage(page);
						}
				}
				else if(accountable == "2nd Year")
				{
					{	
				
						
						
						List<Registered> list = (List<Registered>)sqlMap.queryForList("Registered.getRegisteredByYear", 2);
						Registered reg = null;
					    for (Registered b : list) {
					    	
					    	String id =  b.getStudent_id();
					    	System.out.println(id);
					      	Accountability a = new Accountability();
							a.setAccountability_desc(desc);
							a.setDate_created(ts);
							a.setSchoolyear(schoolyear);
							a.setSemester(semester);
							a.setAmount(amount);
							a.setStatus("not cleared");
							a.setUploader(uploader);
							a.setStudent_id(id);
							sqlMap.insert("Accountability.insert", a);
							
							
							Logs log = new Logs();
							log.setLog_activity("Add Accountabiity:" + id + " " + desc);
							log.setLog_officer(uploader);
							log.setLog_time(ts);
							
							sqlMap.insert("Logs.insert", log);
					      
					    }
					    
					    AccountabilityItem acc = new AccountabilityItem();
						acc.setAccountable(accountable);
						acc.setDate_created(ts);
						acc.setItem_amount(amount);
						acc.setItem_desc(desc);
						acc.setItem_quantity(quantity);
						acc.setItem_schoolyear(schoolyear);
						acc.setItem_sem(semester);
						acc.setUploader(uploader);
						sqlMap.insert("AccountabilityItem.insert", acc);
			
						AccountabilityItemList page = new AccountabilityItemList();
						setResponsePage(page);
						}
				}
				else if(accountable == "3rd Year")
				{
					{	
				
						
						
						List<Registered> list = (List<Registered>)sqlMap.queryForList("Registered.getRegisteredByYear", 3);
						Registered reg = null;
					    for (Registered b : list) {
					    	
					    	String id =  b.getStudent_id();
					    	System.out.println(id);
					      	Accountability a = new Accountability();
							a.setAccountability_desc(desc);
							a.setDate_created(ts);
							a.setSchoolyear(schoolyear);
							a.setSemester(semester);
							a.setAmount(amount);
							a.setStatus("not cleared");
							a.setUploader(uploader);
							a.setStudent_id(id);
							sqlMap.insert("Accountability.insert", a);
							
							
							Logs log = new Logs();
							log.setLog_activity("Add Accountabiity:" + id + " " + desc);
							log.setLog_officer(uploader);
							log.setLog_time(ts);
							
							sqlMap.insert("Logs.insert", log);
					      
					    }
					    
					    AccountabilityItem acc = new AccountabilityItem();
						acc.setAccountable(accountable);
						acc.setDate_created(ts);
						acc.setItem_amount(amount);
						acc.setItem_desc(desc);
						acc.setItem_quantity(quantity);
						acc.setItem_schoolyear(schoolyear);
						acc.setItem_sem(semester);
						acc.setUploader(uploader);
						sqlMap.insert("AccountabilityItem.insert", acc);
			
						AccountabilityItemList page = new AccountabilityItemList();
						setResponsePage(page);
						}
				}
				else if(accountable == "4th Year")
				{
					{	
				
						
						
						List<Registered> list = (List<Registered>)sqlMap.queryForList("Registered.getRegisteredByYear", 4);
						Registered reg = null;
					    for (Registered b : list) {
					    	
					    	String id =  b.getStudent_id();
					    	System.out.println(id);
					      	Accountability a = new Accountability();
							a.setAccountability_desc(desc);
							a.setDate_created(ts);
							a.setSchoolyear(schoolyear);
							a.setSemester(semester);
							a.setAmount(amount);
							a.setStatus("not cleared");
							a.setUploader(uploader);
							a.setStudent_id(id);
							sqlMap.insert("Accountability.insert", a);
							
							
							Logs log = new Logs();
							log.setLog_activity("Add Accountabiity:" + id + " " + desc);
							log.setLog_officer(uploader);
							log.setLog_time(ts);
							
							sqlMap.insert("Logs.insert", log);
					      
					    }
					    
					    AccountabilityItem acc = new AccountabilityItem();
						acc.setAccountable(accountable);
						acc.setDate_created(ts);
						acc.setItem_amount(amount);
						acc.setItem_desc(desc);
						acc.setItem_quantity(quantity);
						acc.setItem_schoolyear(schoolyear);
						acc.setItem_sem(semester);
						acc.setUploader(uploader);
						sqlMap.insert("AccountabilityItem.insert", acc);
			
						AccountabilityItemList page = new AccountabilityItemList();
						setResponsePage(page);
						}
				}
				else
				{
					
					error("Add Accountability Failed. Please Try Again. ");
				}
				
				
				
				
				
					
				} catch (SQLException e) {
					error("Item already exists.");
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					error("Fill up necessary fields.");
				}
				
				}
			
				
			
		});
		
		
		
	
		add(new BookmarkablePageLink("home", TreasurerHome.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("view", AccountabilityItemList.class));
		add(new BookmarkablePageLink("first", ClearancePageFirst.class));
		add(new BookmarkablePageLink("second", ClearancePageSecond.class));
		add(new BookmarkablePageLink("third", ClearancePageThird.class));
		add(new BookmarkablePageLink("fourth", ClearancePageFourth.class));
		add(new FeedbackPanel("error"));
		
		form.add(desc);
		form.add(amount);
		form.add(semester);
		form.add(schoolyear);
		form.add(quantity);
		form.add(accountable);
		add(form);
		
	}

}
